package com.study.sbsummary.service;

import com.study.sbsummary.dao.UserDao;
import com.study.sbsummary.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao mapper;

    @Override
    public List<User> listUsers() {
        return mapper.selectAll();
    }

    @Override
    @Transactional
    public boolean addUser(User user) {
        user.setId(user.getId());
        user.setUsername(user.getUsername());
        user.setPassword(user.getPassword());
        user.setCreateTime(LocalDateTime.now());
        int insert = mapper.insert(user);
        return insert == 1;
    }

    @Override
    public boolean deleteUser(Long id) {
        int delete = mapper.delete(id);
        return delete == 1;
    }

    @Override
    public boolean updateUser(User user) {
        int update = mapper.update(user);
        return update == 1;
    }

    @Override
    public User selectUserById(Long id) {
        User user = mapper.selectById(id);
        return user;
    }
}
